<?php
/**
* @version $Id: Heriniaina Eugene  04/05/2006 17.23 $
* @package mod_annibalquotes
* @copyright Copyright (C) 2006 serasera.org
* @comment This module shows daily quotes from Annibale Difrancia
*/

// no direct access
defined( '_VALID_MOS' ) or die( 'Restricted access' );

$option = trim( mosGetParam( $_REQUEST, 'option', null ) );
$task = trim( mosGetParam( $_REQUEST, 'task', null ) );
$id = intval( mosGetParam( $_REQUEST, 'id', null ) );

global $mosConfig_cachepath;


// use cache if available

require_once( $mosConfig_absolute_path . '/includes/Cache/Lite.php' );

//var_dump($GLOBALS);

$lang = ($_REQUEST["lang"])? $_REQUEST["lang"] : $_COOKIE["mbfcookie"]["lang"];

$annibalquotes_ver = "1.0.6";
$annibalquotes_apikey =  $params->def('annibalquotes_apikey', 'TEST KEY');
$annibalquotes_lang =  $params->def('annibalquotes_lang', $lang );
$annibalquotes_showref =  $params->def('annibalquotes_lang', 0);
$annibalquotes_imagelink =  $params->def('annibalquotes_imagelink', '');
$annibalquotes_imagealign =  $params->def('annibalquotes_imagealign', 'left');

if(!isset($annibalquotes_lang)) $annibalquotes_lang = 'it';

if($annibalquotes_apikey) {
	// set some variables
	$mod_annibalquotes_cache_options = array(
		'cacheDir' => $mosConfig_cachepath . '/',
		'lifeTime' => 86400
	);
	// create a Cache_Lite object
	$mod_annibalquotes_cache_obj = new Cache_Lite($mod_annibalquotes_cache_options);
	$mod_annibalquotes_cache_id =  md5($annibalquotes_apikey . $annibalquotes_lang . $annibalquotes_showref . $annibalquotes_imagelink . $annibalquotes_imagealign);

	$content = "";

	if ($content = $mod_annibalquotes_cache_obj->get($mod_annibalquotes_cache_id)) {
		$content .= "\n<!-- cached " . date("d/m/Y H:i:s") . " -->";
	} else {
		if (!class_exists('nusoapclient')) require_once('mod_annibalquotes/nusoap_mod.php');

		$client = new nusoapclient('http://rcj.org/ws/quotes?wsdl', true);
		$err = $client->getError();
		if ($err) {
			$content = $err ;
		}

		$annibalquotes_Param = array('wsid' => $annibalquotes_apikey, 'lang' => $annibalquotes_lang, 'appver' => $annibalquotes_ver);
		$result = $client->call('getDailyQuote', $annibalquotes_Param);

		// Check for a fault
		if ($client->fault) {
			foreach($result as $key=>$val) {
				$content .= $val . " <br />\n";
			}

		} else {
			// Check for errors
			$err = $client->getError();
			if ($err) {
				// Display the error
				$content .=  $err ;
			} else {
				// Display the result
				$content = "<div class='quote'>";
				if($annibalquotes_imagelink) {
					$content .= '<img src="' . $annibalquotes_imagelink . '" align="' . $annibalquotes_imagealign . '" />';
				}
				$content .= $result[ 'quote' ] . "</div>";
				if($annibalquotes_showref == 1) {
					$content .= "<div class='quote'>" . $result[ 'source' ] . "</div>";
				}
				$mod_annibalquotes_cache_obj->save($content, $mod_annibalquotes_cache_id);
			}
		}
	}
	
}

?>